package com.demo1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/asyncnio",asyncSupported=true)
public class AsyncServletNIO extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<title>非祖塞IO示例</title>");
		out.println("进入Servlet的时间:"+new Date()+"<br/>");
		//创建AsyncContext，开始异步调用
		AsyncContext actx = request.startAsync();
		//设置异步调用的超时时长
		actx.setTimeout(30*1000);
		ServletInputStream input = request.getInputStream();
		//为输入流注册监听器
		input.setReadListener(new MyReadListener(input,actx));
		out.println("结束Servlet的时间:"+new Date()+"<br/>");
		out.flush();
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
